/*
 *  tfreader.cpp
 *  Perceel
 *
 *  Created by Nicola Gigante on 25/04/08.
 *  Copyright 2008 Nicola Gigante. All rights reserved.
 *
 */

#include "tfreader.h"

TFReader::TFReader(QIODevice *d)
{
	doc = QDomDocument("perceel");
	doc.setContent(d);
}

QList<TF> TFReader::read()
{
	QList<TF> l;
	
	QDomElement docElem = doc.documentElement();
	
	if(docElem.tagName() != "perceel")
		throw QString("Bad root tag");
	
	QDomNodeList nodes = docElem.elementsByTagName("function");
	for(int i = 0; i < nodes.count(); i++)
	{
		TF tf;
		
		QDomNodeList roots = nodes.at(i).toElement().elementsByTagName("roots").at(0).toElement().elementsByTagName("root");
		
		for(int j = 0; j < roots.count(); j++)
		{
			Real real = roots.at(j).toElement().attribute("real", "0").toDouble();
			Real imag = roots.at(j).toElement().attribute("imag", "0").toDouble();
			
			Complex c(real, imag);
			tf.addRoot(c);
		}
		
		QDomNodeList poles = nodes.at(i).toElement().elementsByTagName("poles").at(0).toElement().elementsByTagName("pole");
		
		for(int j = 0; j < poles.count(); j++)
		{
			Real real = poles.at(j).toElement().attribute("real", "0").toDouble();
			Real imag = poles.at(j).toElement().attribute("imag", "0").toDouble();
			
			Complex c(real, imag);
			tf.addPole(c);
		}
		
		l.append(tf);
	}
	
	return l;
}

